package cn.sdhuijin.utils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import cn.sdhuijin.model.Branch;
import cn.sdhuijin.model.FTP;

public class CommonUtil {

	private static int num = 0;

	/**
	 * 格式化日期类型
	 * 
	 * @param date
	 *            数据库日期
	 * @return 格式化后日期(yyyy-MM-dd HH:mm:ss)
	 */
	public static String formatDate(Date date) {
		DateFormat format = null;
		String formatDate = "";
		try {
			format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			formatDate = format.format(date);
		} catch (Exception e) {

		}
		return formatDate;
	}

	/**
	 * 获取当前机构下属子机构
	 * 
	 * @param branch
	 *            当前机构
	 * @return 下属子机构ID
	 */
	public static List<Long> getBranchChildrenIds(Branch branch) {
		List<Long> branchIds = new ArrayList<Long>();
		branchIds.add(branch.getId());
		List<Branch> children = branch.getChildren();
		if (children != null && !children.isEmpty()) {
			for (Branch b : children) {
				branchIds.addAll(getBranchChildrenIds(b));
			}
		}
		return branchIds;
	}

	/**
	 * 判断字符串数组中的所有字符串是否为空
	 *
	 * @param str
	 *            字符串数组
	 * @return 字符串都不为空，返回true；否则，返回false
	 */
	public static boolean allIsNotEmpty(String... str) {
		Boolean flag = true;
		for (String s : str) {
			if (s == null || s.trim().equals("")) {
				flag = false;
			}
		}
		return flag;
	}

	/**
	 * String convert to InputStream
	 * 
	 * @param str
	 * @return
	 */
	public static InputStream getInputStreamFromString(String str) {
		InputStream in = new ByteArrayInputStream(str.getBytes());
		return in;
	}

	/**
	 * 生成六位随机数字
	 * 
	 * @return
	 */
	public static String GenerateSixRandomNum() {
		Random random = new Random();
		int x = random.nextInt(899999);
		x = x + 100000;
		return String.valueOf(x);
	}

	/**
	 * 在一定范围内生成随机数
	 * 
	 * 例如生成0（包含）~3（不包含）之间的数即（0、1、2），则min = 0， max=3
	 * 
	 * @param min
	 * @param max
	 * @return
	 */
	public static int GenerateRandomNum(int min, int max) {
		int s = 0;
		if (min < max) {
			Random random = new Random();
			s = random.nextInt(max) % (max - min + 1) + min;
		}
		return s;
	}

	/**
	 * 通过ftp上传文件
	 * 
	 * @param telephone
	 *            手机号
	 * @param content
	 *            内容
	 * @return
	 */
	public static synchronized Boolean sendMsg(String telephone, String content) {
		Boolean flag = false;
		try {
			FTPUtil fu = new FTPUtil();
			FTP ftp = new FTP();
			ftp.load();
			fu.connectFTPServer(ftp.getFtp(), ftp.getPort(), ftp.getName(),
					ftp.getPassword());
			InputStream in = CommonUtil.getInputStreamFromString("unknow|"
					+ telephone + "|me|" + content + "|0|");
			if (num == 999999) {
				num = 0;
			}
			num++;
			String seqNo = String.format("%06d", num);
			fu.uploadFile(in, "904000000_000000000000_" + seqNo + "_alarm.txt",
					ftp.getFtpDir());
			flag = true;
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

}
